﻿// DataProductLibraryDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "DataProductLibraryDialog.h"
#include "afxdialogex.h"

#include "DataProductPropertyDdialog.h"


// CDataProductLibraryDialog 对话框

IMPLEMENT_DYNAMIC(CDataProductLibraryDialog, CDialogEx)

CDataProductLibraryDialog::CDataProductLibraryDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_DATA_PRODUCT_LIBRARY, pParent)
{
	m_nProductCode = -1;
}

CDataProductLibraryDialog::~CDataProductLibraryDialog()
{
}

void CDataProductLibraryDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_props);
	DDX_Control(pDX, IDC_LIST_DATA, m_list);
}


BEGIN_MESSAGE_MAP(CDataProductLibraryDialog, CDialogEx)
	ON_WM_CREATE()
	ON_NOTIFY(NM_CLICK, IDC_LIST_DATA, &CDataProductLibraryDialog::OnNMClickListData)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_DATA, &CDataProductLibraryDialog::OnNMDblclkListData)
END_MESSAGE_MAP()


// CDataProductLibraryDialog 消息处理程序


int CDataProductLibraryDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	return 0;
}


BOOL CDataProductLibraryDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_list.AttachBitmap();
	m_list.Populate();

	AdjustLayout();
	if (m_list.GetItemCount() > 0)
	{
		PopulateDataProductProperty(0);
	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CDataProductLibraryDialog::AdjustLayout()
{
	CWnd* pWnd = GetDlgItem(IDC_STATIC_PROPS);
	if (pWnd)
	{
		CRect rect;
		GetDlgItem(IDC_STATIC_PROPS)->GetWindowRect(rect);
		ScreenToClient(rect);
		int margin = 1;
		rect.left += margin;
		rect.right -= margin;
		rect.top += margin;
		rect.bottom -= margin;
		m_props.MoveWindow(rect);
	}
}

void CDataProductLibraryDialog::OnNMClickListData(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}
	PopulateDataProductProperty(nItem);

	*pResult = 0;
}


void CDataProductLibraryDialog::PopulateDataProductProperty(int nItem)
{
	//m_nProductCode = m_list.GetItemData(nItem);
	m_nProductCode = nItem;

	ck::DataProductInfo* pProduct = NULL;
	ck::DataProductManager* pProductManager = ck::DataProductManager::GetInstance();
	pProduct = pProductManager->GetDataProduct(m_nProductCode);
	if (pProduct)
	{
		m_props.Populate(pProduct);

		//pProduct->Release();
	}
}

void CDataProductLibraryDialog::OnNMDblclkListData(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}

	m_nProductCode = nItem;

	ck::DataProductInfo* pProduct = NULL;
	ck::DataProductManager* pProductManager = ck::DataProductManager::GetInstance();
	pProduct = pProductManager->GetDataProduct(m_nProductCode);
	if (pProduct)
	{
		CDataProductPropertyDdialog dlg;
		dlg.SetProduct(pProduct);
		dlg.DoModal();
	}

	*pResult = 0;
}
